//
//  YWMoreRecordTableViewCell.m
//  maintain
//
//  Created by Kyo on 16/5/31.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWMoreRecordTableViewCell.h"
#import "UIColor+CustomColors.h"
#import "YWMacro.h"
#import "YWCommonTool.h"

#import "YWInspectionLineRecord.h"
#import "YWInspectionMissionModel.h"
#import "YWInspectionMissionManager.h"

@interface YWMoreRecordTableViewCell ()
@property (nonatomic, strong) UILabel *patrolLineNameLabel;
@property (nonatomic, strong) UILabel *statusLabel;
@property (nonatomic, strong) UILabel *handlerLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@end
@implementation YWMoreRecordTableViewCell

+ (instancetype)cellWithTableView:(UITableView*)tableView{
    static NSString* identifier = @"YWMoreRecordTableViewCellIdentifier";
    YWMoreRecordTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[YWMoreRecordTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.accessoryType = UITableViewCellAccessoryNone;
        self.opaque = YES;
        [self setUpeSubViews];
    }
    return self;
}

- (UILabel *)patrolLineNameLabel{
    if (!_patrolLineNameLabel) {
        UILabel *patrolLineNameLabel = [[UILabel alloc]init];
        patrolLineNameLabel.font = kContentFont;
        [self.contentView addSubview:patrolLineNameLabel];
        _patrolLineNameLabel = patrolLineNameLabel;
    }
    return _patrolLineNameLabel;
}

- (UILabel *)statusLabel{
    if (!_statusLabel) {
        UILabel *statusLabel = [[UILabel alloc]init];
        statusLabel.font = KSmallContentFont;
        statusLabel.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:statusLabel];
        _statusLabel = statusLabel;
    }
    return _statusLabel;
}

- (UILabel *)handlerLabel{
    if (!_handlerLabel) {
        UILabel *handlerLabel = [[UILabel alloc]init];
        handlerLabel.font = kContentFont;
        [self.contentView addSubview:handlerLabel];
        _handlerLabel = handlerLabel;
    }
    return _handlerLabel;
}

- (UILabel *)timeLabel{
    if (!_timeLabel) {
        UILabel *timeLabel = [[UILabel alloc]init];
        timeLabel.font = KSmallContentFont;
        timeLabel.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:timeLabel];
        _timeLabel = timeLabel;
    }
    return _timeLabel;
}

- (void)setUpeSubViews
{
    CGFloat statusLabelW = 100;
    CGFloat nameLabelW = SCREEN_WIDTH-kMarginW*3-statusLabelW;
    CGFloat handlerLabelW = nameLabelW;
    CGFloat timeLabelW = statusLabelW;
    [self.patrolLineNameLabel setFrame:CGRectMake(kMarginW, 10, nameLabelW, kLabelH)];
    [self.statusLabel setFrame:CGRectMake(CGRectGetMaxX(_patrolLineNameLabel.frame)+kMarginW, 10, statusLabelW, kLabelH)];
    [self.handlerLabel setFrame:CGRectMake(kMarginW, 10+kLabelH, handlerLabelW, kLabelH)];
    [self.timeLabel setFrame:CGRectMake(CGRectGetMaxX(_handlerLabel.frame)+kMarginW, 10+kLabelH, timeLabelW, kLabelH)];
}

- (void)setLineRecord:(YWInspectionLineRecord *)lineRecord{
    _lineRecord = lineRecord;
    
    if (lineRecord.inspectionLineId == 0) {
        self.patrolLineNameLabel.text = NSLocalizedString(@"tempInspection", nil);//@"临时巡查";
    }else{
        YWInspectionLineModel *lineModel = [[YWInspectionMissionManager shareManager] inspectionLineWithLineId:lineRecord.inspectionLineId];
        self.patrolLineNameLabel.text = lineModel.inspectionLineName;
    }
    
    NSString *result = @"";
    BOOL noExecutor = NO;
    if (lineRecord.result==YWInspectionLineRecordResultUnfinishAndNormal){
        result = NSLocalizedString(@"unFinished", nil);//@"未完成";
        self.statusLabel.textColor = [UIColor blackColor];
    }else if (lineRecord.result==YWInspectionLineRecordResultUnfinishAndTrouble) {
        result = NSLocalizedString(@"unFinishedAndTrouble", nil);//@"未完成(有报障)";
        self.statusLabel.textColor = [UIColor customRedColor];
    }else if (lineRecord.result==YWInspectionLineRecordResultFinishAndTrouble){
        result = NSLocalizedString(@"hasFinishedAndTrouble", nil);//@"已完成(有报障)";
        self.statusLabel.textColor = [UIColor customRedColor];
    }else if (lineRecord.result==YWInspectionLineRecordResultFinishAndNormal) {
        result = NSLocalizedString(@"isOver", nil);//@"已完成";
        self.statusLabel.textColor = [UIColor customTextGrayColor];
    }else{
        result = NSLocalizedString(@"unFinished", nil);//@"未完成";//@"无人巡查";
        self.statusLabel.textColor = [UIColor blackColor];
        noExecutor = YES;
    }
    self.statusLabel.text = result;
    
    if (noExecutor) {
        self.handlerLabel.text = NSLocalizedString(@"noManToInspection", nil);//@"无人巡查";
    }else{
        NSString *handerName = [YWCommonTool handlerNamesWithHandlers:lineRecord.executors];
        self.handlerLabel.text = handerName.length?handerName:NSLocalizedString(@"noManToInspection", nil);
    }
    
    NSString *startTime = [YWCommonTool showTime:lineRecord.startTime/1000 andFormatter:KTimeFormatShortTime];
    NSString *endTime = [YWCommonTool showTime:lineRecord.endTime/1000 andFormatter:KTimeFormatShortTime];
    self.timeLabel.text = [NSString stringWithFormat:@"%@-%@",startTime,endTime];
}

@end
